---
title: "Exploratory Analysis of Daily EMA Data"
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: scroll
source: embed
---
```{r setup, include=FALSE}
# Setup options
knitr::opts_chunk$set(collapse = TRUE, comment = "")
# CRAN packages
library(flexdashboard)
library(dplyr)
library(knitr)
library(tidyr)
library(ggplot2)
# devtools::install_github("mbcann01/dataclean")
library(dataclean)
# Load ea_kable
source("/Users/bradcannell/Dropbox/Research/mChat/R scripts/ea_kable.R")
# Load data
load("/Users/bradcannell/Dropbox/Research/mChat/data/daily_april_2016.RData")
# Sort by case number and date
daily <- dplyr::arrange(daily, case_number, date)
```
Overview {.sidebar data-width=300}
===============================================================================
Here we plot the aggregate unconditional distribution of responses for each of the following variables:
1. Feelings at time of assessment
2. Location at time of assessment
3. Activity at time of assessment
4. Physical activity yesterday
5. Diet yesterday
6. Medication use yesterday
7. Freetime activities yesterday
8. Meaningful Interaction yesterday
9. Substance use yesterday
```{r overview}
obs <- nrow(daily)
vars <- ncol(daily)
subj <- length(base::unique(daily$case_number))
n_per_subj <- count(daily, case_number)
avg_per_subj <- round(mean(n_per_subj$n), 0)
max_n_subj <- max(n_per_subj$n)
min_n_subj <- min(n_per_subj$n)
cat(paste("The dataset contains: \n",
obs, "Observations \n",
vars, "Variables \n",
subj, "Unique participants \n",
"With an average of", avg_per_subj, "\n", "observations each \n",
"Max =", max_n_subj, "observations \n",
"Min =", min_n_subj, "observation")
)
```
Feeling
===============================================================================
Feelings Table
-------------------------------------------------------------------------------
### Feelings at time of assessment
```{r feeling_table}
feelings <- select(daily, happy:sluggish)
vars <- tools::toTitleCase(names(feelings))
ea_kable(
x = feelings,
xlab = vars,
nrows = 10,
ncols = 6,
colnames = c("Feeling", "Strongly Disagree", "Disagree", "Neutral", "Agree",
"Strongly Agree")
)
```
Feelings Charts
-------------------------------------------------------------------------------
```{r plot_emotions}
for (var in vars) {
plot <- ggplot(daily, aes_string(x = tolower(var))) +
geom_bar() +
scale_x_discrete("") +
ggtitle(paste("I Feel", var, "Right Now")) +
theme_bw()
print(plot)
}
```
Location
===============================================================================
Location Table
-------------------------------------------------------------------------------
### Location at time of assessment
```{r location_table}
x <- select(daily, loc_bus:loc_work)
loc <- c("Bus, Train, or Vehicle", "Church", "Home", "Friend's or Relative's House", "Store / Mall", "Outdoors", "Other", "Restaurant", "School / Library", "Work")
ea_kable(
x = x,
xlab = loc,
nrows = 10,
ncols = 3,
colnames = c("Location", "No", "Yes")
)
```
Location Charts
-------------------------------------------------------------------------------
```{r plot_location, fig.width=12}
# Summarize
x <- data.frame(sapply(x, table))
# Tidy data
x <- gather(x)
# Keep even numbered rows
x <- x[c(FALSE, TRUE), ]
# Improve readability
x$key <- loc
# Plot the data
ggplot(x, aes(x = key, y = value)) +
geom_bar(stat = "identity") +
scale_x_discrete("") +
scale_y_continuous("Number of Responses") +
ggtitle("Location at Time of Assessment") +
theme_bw()
```
Activity
===============================================================================
Activity Table
-------------------------------------------------------------------------------
### Activity at time of assessment
```{r activity_table}
x <- select(daily, pre_who, act_sit:act_talk, act_other)
act <- c("Interacting With Someone", "Sitting", "Sleeping", "Standing", "Walking / Excercising", "Talking", "Other")
ea_kable(
x = x,
xlab = act,
nrows = 7,
ncols = 3,
colnames = c("Activity", "No", "Yes")
)
```
Activity Charts
-------------------------------------------------------------------------------
```{r plot_act, fig.width=12}
x <- data.frame(sapply(x, table))
x <- gather(x)
x <- x[c(FALSE, TRUE), ]
x$key <- act
# Plot the data
ggplot(x, aes(x = key, y = value)) +
geom_bar(stat = "identity") +
scale_x_discrete("") +
scale_y_continuous("Number of Responses") +
ggtitle("Activity Immediately Prior to Assessment") +
theme_bw()
```
Physical Activity
===============================================================================
Table Row {.tabset .tabset-fade}
-------------------------------------------------------------------------------
### Physical activity yesterday (Yes / No)
```{r physact_table_1}
x <- select(daily, yest_bike, yest_run, yest_cleaning, yest_none, pedometer)
act <- c("Walked or Biked to Get Somewhere", "Engaged in Physical Fitness", "Engaged in Physical Activity at Work or Home", "Did None of These", "Used Pedometer")
ea_kable(
x = x,
xlab = act,
nrows = 5,
ncols = 3,
colnames = c("Activity", "No", "Yes")
)
```
### Physical activity yesterday (Minutes)
```{r physact_table_2}
x <- select(daily, min_walk, min_run, act_min)
act <- c("Walked or Biked to Get Somewhere", "Engaged in Physical Fitness", "Engaged in Physical Activity at Work or Home")
ea_kable(
x = x,
xlab = act,
nrows = 3,
ncols = 7,
colnames = c("Activity", "10 Minutes or Less", "11-20 Minutes", "21-30 Minutes",
"31-40 Minutes", "41-50 Minutes", "More than 50 Minutes")
)
```
### Sitting time yesterday
```{r sitting_table}
x <- select(daily, min_sit)
ea_kable(
x = x,
xlab = "Sitting Time Yesterday",
nrows = 1,
ncols = 7,
colnames = c("Variable", "4 or Fewer Hours", "More than 4 Hours- 6 Hours",
"More than 6 Hours- 8 Hours", "More than 8 Hours- 10 Hours",
"More than 10 Hours - 12 Hours", "More than 12 Hours")
)
```
Physical Activity Charts
-------------------------------------------------------------------------------
```{r plot_act_yest}
x <- names(select(daily, yest_bike, yest_run, yest_cleaning, yest_none, pedometer, min_walk, min_run, act_min, min_sit))
act <- c("Walked or Biked to Get Somewhere", "Engaged in Physical Fitness", "Engaged in Physical Activity at Work or Home", "Did None of These", "Used Pedometer", "Walked or Biked to Get Somewhere", "Engaged in Physical Fitness", "Engaged in Physical Activity at Work or Home", "Sat")
i <- 1
for (var in x) {
plot <- ggplot(daily, aes_string(x = var)) +
geom_bar() +
scale_x_discrete("") +
ggtitle(paste(act[i], "Yesterday")) +
theme_bw() +
if (i > 5) {
theme(axis.text.x = element_text(angle = 45, vjust = 0.5))
} else {
theme()
}
print(plot)
i <- i + 1
}
# Plot steps
ggplot(daily, aes(x = steps)) +
geom_histogram(binwidth = 1000) +
scale_x_continuous("") +
ggtitle("Steps Yesterday") +
theme_bw()
```
Diet
===============================================================================
Table Row {.tabset .tabset-fade}
-------------------------------------------------------------------------------
### Ate any yesterday (Yes / No)
```{r diet_table_1}
x <- select(daily, any_fruit, any_veg, any_ssb, any_sweets, any_meat)
food <- c("Fruit", "Vegetables", "Sugar-Sweetened Beverages", "Deserts and Other Sweets", "Red Meat or Processed Meat")
ea_kable(
x = x,
xlab = food,
nrows = 5,
ncols = 3,
colnames = c("Food", "No", "Yes")
)
```
### Servings ate yesterday
```{r diet_table_2}
x <- select(daily, serv_fruit, serv_veg, serv_ssb, serv_sweets, serv_meat)
food <- c("Fruit", "Vegetables", "Sugar-Sweetened Beverages", "Deserts and Other Sweets", "Red Meat or Processed Meat")
ea_kable(
x = x,
xlab = food,
nrows = 5,
ncols = 7,
colnames = c("Food", "0 Servings", "1 Serving", "2 Servings", "3 Servings", "4 Servings",
"5 or More Servings")
)
```
Diet Charts
-------------------------------------------------------------------------------
```{r diet_yest}
x <- names(select(daily, any_fruit, any_veg, any_ssb, any_sweets, any_meat, serv_fruit, serv_veg, serv_ssb, serv_sweets, serv_meat))
food <- c("Fruit", "Vegetables", "Sugar-Sweetened Beverages", "Deserts and Other Sweets", "Red Meat or Processed Meat", "Fruit", "Vegetables", "Sugar-Sweetened Beverages", "Deserts and Other Sweets", "Red Meat or Processed Meat")
i <- 1
for (var in x) {
plot <- ggplot(daily, aes_string(x = var)) +
geom_bar() +
scale_x_discrete("") +
ggtitle(paste("Ate", food[i], "Yesterday")) +
theme_bw()
print(plot)
i <- i + 1
}
```
Medication
===============================================================================
Medication Table {.tabset .tabset-fade}
-------------------------------------------------------------------------------
### Did you take all your medication as prescribed yesterday, and if not, which ones didn't you take?
```{r med_table_1}
x <- select(daily, take_meds, medtype_dep, medtype_psy, medtype_ast, medtype_bp, medtype_diab, medtype_pain, medtype_chol, medtype_anti, medtype_other)
xlab <- c("Take Medications", "Didn't Take Depression / Anxiety / Mood Medication", "Didn't Take Psychiatric Medication", "Didn't Take Asthma / COPD Medication", "Didn't Take Blood Pressure Medication", "Didn't Take Diabetes Medication", "Didn't Take Pain Medication", "Didn't Take Cholesterol Medication", "Didn't Take Antibiotic / Antiviral Medication", "Didn't Take Other Medication")
ea_kable(
x = x,
xlab = xlab,
nrows = 10,
ncols = 3,
colnames = c("Medication", "No", "Yes")
)
```
### Why didn't you take medications?
```{r med_table_2}
x <- select(daily, meds_ran_out, meds_forgot, meds_no_need, meds_side, meds_lost, meds_other)
xlab <- c("Ran out", "Forgot", "Don't Need It", "Side Effects", "Got Lost / Stolen", "Other")
ea_kable(
x = x,
xlab = xlab,
nrows = 6,
ncols = 3,
colnames = c("Reason", "Wasn't a Reason", "Was a Reason")
)
```
Medication Charts
-------------------------------------------------------------------------------
```{r med_chart, fig.width=12}
# Take Meds (Yes / No)
x <- select(daily, take_meds)
ggplot(x, aes(x = take_meds)) +
geom_bar() +
scale_x_discrete("") +
ggtitle("Take All Medications as Prescribed Yesterday") +
theme_bw()
# Which medications not taken
x <- select(daily, medtype_dep, medtype_psy, medtype_ast, medtype_bp, medtype_diab, medtype_pain, medtype_chol, medtype_anti, medtype_other)
xlab <- c("Depression / Anxiety / Mood", "Psychiatric", "Asthma / COPD", "Blood Pressure", "Diabetes", "Pain", "Cholesterol", "Antibiotic / Antiviral", "Other")
x <- data.frame(sapply(x, table))
x <- gather(x)
x <- x[c(FALSE, TRUE), ]
x$key <- xlab
ggplot(x, aes(x = key, y = value)) +
geom_bar(stat = "identity") +
scale_x_discrete("") +
scale_y_continuous("Number of Responses") +
ggtitle("Medication Not Taken Yesterday") +
theme_bw()
# Why medication not taken
x <- select(daily, meds_ran_out, meds_forgot, meds_no_need, meds_side, meds_lost, meds_other)
xlab <- c("Ran out", "Forgot", "Don't Need It", "Side Effects", "Got Lost / Stolen", "Other")
x <- data.frame(sapply(x, table))
x <- gather(x)
x <- x[c(FALSE, TRUE), ]
x$key <- xlab
ggplot(x, aes(x = key, y = value)) +
geom_bar(stat = "identity") +
scale_x_discrete("") +
scale_y_continuous("Number of Responses") +
ggtitle("Reason Medication Not Taken Yesterday") +
theme_bw()
```
Freetime
===============================================================================
Freetime Table {.tabset .tabset-fade}
-------------------------------------------------------------------------------
### How much free time did you have yesterday?
```{r freetime_table_1}
x <- select(daily, free_time)
xlab <- c("Amount")
ea_kable(
x = x,
xlab = xlab,
nrows = 1,
ncols = 7,
colnames = c("Free time", "2 or fewer hours", "More than 2 hours- 4 hours",
"More than 4 hours- 6 hours", "More than 6 hours- 8 hours", "More than 8 hours- 10 hours",
"More than 10 hours")
)
```
### What did you do in your free time yesterday?
```{r freetime_table_2}
x <- select(daily, free_tv:free_social, free_none)
xlab <- c("Watched TV", "Played Computer Games", "Went to a Movie", "Surfed the Internet", "Read", "Listened to Music or Played Instrument", "Arts and Crafts", "Cleaned", "Took Care of Pets", "Prayed, Meditated, or Went to Religious Service", "Played a Sport", "Walk or Jog", "Played Cards, Dice, or Board Games", "Went Shopping", "Volunteered", "Socialized", "None of These")
ea_kable(
x = x,
xlab = xlab,
nrows = 17,
ncols = 3,
colnames = c("Activity", "Didn't Do Yesterday", "Did Yesterday")
)
```
Freetime Charts
-------------------------------------------------------------------------------
```{r freetime_chart, fig.width=12, fig.height=8}
# How much
x <- select(daily, free_time)
xlab <- c("Amount")
i <- 1
for (var in x) {
plot <- ggplot(daily, aes_string(x = var)) +
geom_bar() +
scale_x_discrete("") +
ggtitle("Amount of Free Time Yesterday") +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, vjust = 0.5))
print(plot)
i <- i + 1
}
# What they did during free time
x <- select(daily, free_tv:free_social, free_none)
xlab <- c("Watched TV", "Played Computer Games", "Went to a Movie", "Surfed the Internet", "Read", "Listened to Music or Played Instrument", "Arts and Crafts", "Cleaned", "Took Care of Pets", "Prayed, Meditated, or Went to Religious Service", "Played a Sport", "Walk or Jog", "Played Cards, Dice, or Board Games", "Went Shopping", "Volunteered", "Socialized", "None of These")
x <- data.frame(sapply(x, table))
x <- gather(x)
x <- x[c(FALSE, TRUE), ]
x$key <- xlab
ggplot(x, aes(x = key, y = value)) +
geom_bar(stat = "identity") +
scale_x_discrete("") +
scale_y_continuous("Number of Responses") +
ggtitle("Free Time Activity Yesterday") +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, vjust = 0.5))
```
Interaction
===============================================================================
Interaction Table {.tabset .tabset-fade}
-------------------------------------------------------------------------------
### Any meaningful interaction yesterday
```{r interaction_table_1}
x <- select(daily, any_talk, any_group)
xlab <- c("One-On-One Conversations", "Group Interactions")
ea_kable(
x = x,
xlab = xlab,
nrows = 2,
ncols = 3,
colnames = c("Type", "No", "Yes")
)
```
### Amount of meaningful interaction yesterday
```{r interaction_table_2}
x <- select(daily, min_talk, min_group)
xlab <- c("One-On-One Conversations", "Group Interactions")
ea_kable(
x = x,
xlab = xlab,
nrows = 2,
ncols = 8,
colnames = c("Type", "15 or Fewer Minutes", "16-30 Minutes", "31 min- 1 Hour",
"More than 1 Hour- 2 Hours", "More than 2 Hours- 3 Hours", "More than 3 Hours- 4 Hours",
"More than 4 Hours")
)
```
Interaction Charts
-------------------------------------------------------------------------------
```{r interaction_chart}
# Any interaction
x <- select(daily, any_talk, any_group)
xlab <- c("One-On-One Conversations", "Group Interactions")
i <- 1
for (var in x) {
plot <- ggplot(daily, aes_string(x = var)) +
geom_bar() +
scale_x_discrete("") +
ggtitle(paste(xlab[i], "Yesterday")) +
theme_bw()
print(plot)
i <- i + 1
}
# Amount of interaction
x <- select(daily, min_talk, min_group)
xlab <- c("One-On-One Conversations", "Group Interactions")
i <- 1
for (var in x) {
plot <- ggplot(daily, aes_string(x = var)) +
geom_bar() +
scale_x_discrete("") +
ggtitle(paste("Amount of", xlab[i], "Yesterday")) +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, vjust = 0.5))
print(plot)
i <- i + 1
}
```
Substances
===============================================================================
Substances Tables
-------------------------------------------------------------------------------
### Substances used yesterday
```{r substance_table}
x <- select(daily, sub_alc, heavy_drink, sub_cig, sub_mar, sub_opi, sub_stim, sub_herb, sub_other, sub_none)
xlab <- c("Alcohol", "5 or More Alcoholic Drinks", "Tobacco", "Marijuana", "Opiates", "Stimulants", "Herbal Drugs / Incense", "Another drug", "None")
ea_kable(
x = x,
xlab = xlab,
nrows = 9,
ncols = 3,
colnames = c("Type", "No", "Yes")
)
```
Substances Charts
-------------------------------------------------------------------------------
```{r substance_chart}
x <- names(select(daily, sub_alc, heavy_drink, sub_cig, sub_mar, sub_opi, sub_stim, sub_herb, sub_other, sub_none))
sub <- c("Alcohol", "Tobacco", "Marijuana", "Opiates", "Stimulants", "Herbal Drugs", "Another drug", "No Substances")
i <- 1
for (var in x) {
plot <- ggplot(daily, aes_string(x = var)) +
geom_bar() +
scale_x_discrete("") +
ggtitle(paste("Participant used", sub[i], "Yesterday")) +
theme_bw()
print(plot)
i <- i + 1
}
```
Session Info
===============================================================================
```{r session_info, echo=FALSE}
sessionInfo()
```